﻿package velbus.modules 
{
	import flash.events.MouseEvent;
	import flash.utils.ByteArray;
	import mx.controls.Button;
	import velbus.modules.part.Switch;
	import velbus.Packet;
	import velbus.Velbus;
	import velbus.VelbusEvent;
	
	/**
	* ...
	* @author Gert Stalpaert
	*/
	public class PushButton8Ch extends VelbusModule
	{

		
		public static const MODULE_TYPE:int = 0x01;
		private var _nameListCounter:int = 0;;
			
		
		public function PushButton8Ch(velbus:Velbus, address:int) 
		{
			super(velbus, address);
			title = "8 Channel Push Button " + Velbus.dec2hex(_address)
			addEventListener(VelbusModuleEvent.NAME_LIST_CHANGE, onNameListChange);
		}
		
		private function onNameListChange(e:VelbusModuleEvent):void 
		{
			//delete previous
			this.removeAllChildren();
			_nameListCounter++
			if (_nameListCounter == NrOfNames * 3) { //only add switches if all name data is read
				for (var i:int = 0; i < _nameList.length; i++) 
				{
					var sw:Switch = new Switch(_velbus, _address, i, _nameList[i]);
					this.addChild(sw);
				}
				///request status to fill the switch statusses
				var ba:ByteArray = new ByteArray();
				ba[0] = VelbusModule.REQUEST_MODULE_STATUS;
				ba[1] = 0xFF // ask for all 8 buttons
				Velbus.addToSendQueue(new Packet(_address, Packet.PRIORITY_LOW, 2, ba, false));
			}
		}
		
		override protected function get NrOfNames():int
		{
			return 8;
		}
		
		
	}
	
}